package qing.service;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import qing.common.des;
import qing.dao.PersonDao;
import qing.entity.Person;

@Service
public class personServiceImpl extends ServiceImpl<Person> implements personService {




	@SuppressWarnings("unchecked")
	@Override
	public Person getPersonByAccount(String account) {
		String hql = "select p from Person p where p.username=:name and deleted=false";
		Query query = createQuery(hql).setParameter("name", account);
		return (Person) query.uniqueResult();
	}

	@Override
	public void create(Person person) {
		if (getPersonByAccount(person.getUsername()) != null) {
			throw new RuntimeException("账户" + person.getAccount() + "已存在！");
		}
		person.setPassword(des.md5(person.getPassword()));
		super.create(person);
	}



	@SuppressWarnings("unchecked")
	@Override
	public Person getPerson(String username, String password) {
		String hql = "select p from Person p where p.username=? and p.password=? and p.deleted=false";
		List<Person> list = createQuery(hql).setParameter(0, username).setParameter(1, password).list();
		if (list.size() > 0)
			return (Person) list.get(0);
		return null;
	}
}
